package com.zhuozhengsoft.pageoffice6springboot2back.controller;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.excel.ExcelTableWriter;
import com.zhuozhengsoft.pageoffice.excel.SheetWriter;
import com.zhuozhengsoft.pageoffice.excel.WorkbookWriter;
import com.zhuozhengsoft.pageoffice6springboot2back.util.GetDirPathUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/SetXlsTableByUser/")
public class SetXlsTableByUserController {

    //获取doc目录的磁盘路径
    private final String dir = GetDirPathUtil.getDirPath() + "/static/doc/";

    @RequestMapping(value = "Excel")
    public Map showWord(HttpServletRequest request, String userName) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);

        WorkbookWriter wb = new WorkbookWriter();
        SheetWriter sheet = wb.openSheet("Sheet1");
        ExcelTableWriter tableA = sheet.openTable("C4:D6");
        ExcelTableWriter tableB = sheet.openTable("C7:D9");

        tableA.setSubmitName("tableA");
        tableB.setSubmitName("tableB");

        //根据登录用户名设置数据区域可编辑性
        String strInfo = "";

        //A部门经理登录后
        if (userName.equals("zhangsan")) {
            strInfo = "A部门经理，所以只能编辑A部门的产品数据";
            tableA.setReadOnly(false);
            tableB.setReadOnly(true);
        }
        //B部门经理登录后
        else {
            strInfo = "B部门经理，所以只能编辑B部门的产品数据";
            tableA.setReadOnly(true);
            tableB.setReadOnly(false);
        }

        poCtrl.setWriter(wb);

        poCtrl.webOpen("/doc/SetXlsTableByUser/test.xlsx", OpenModeType.xlsSubmitForm, userName);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("pageoffice", poCtrl.getHtml());
        map.put("strInfo", strInfo);
        return map;
    }


    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(dir + "SetXlsTableByUser/" + fs.getFileName());
        fs.close();
    }

}
